<?php

namespace App\Http\Controllers\Web\Seller;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Repositories\Interfaces\ExpressCompanyRepository;
use App\Repository\Repositories\Interfaces\OrderProductsRepository;
use Illuminate\Http\Request;

/**
 * 快递公司管理控制器
 *
 * @package App\Http\Controllers\Backend
 */
class ExpressCompanyController extends BaseController
{
    /**
     * @var ExpressCompanyRepository
     */
    protected $express_company_repo;

    protected $user_id;

    public function __construct(ExpressCompanyRepository $express_company_repo)
    {
        $this->express_company_repo = $express_company_repo;
    }

    public function index(Request $request)
    {
        $this->validate($request, [
            'type' => 'string|in:page,all,kv'
        ]);
        $type = $request->get('type', 'page');
        if($type == 'all'){ //全部
            $express_companys = $this->express_company_repo->orderBy('sort', 'desc')->all();
        }elseif($type == 'kv'){ //键值对
            $express_companys = $this->express_company_repo->orderBy('sort', 'desc')->pluck('short_name', 'company_code');
        }else{  //分页
            $express_companys = $this->express_company_repo->orderBy('sort', 'desc')->paginate($this->perPage);
        }

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $express_companys, true);
    }

    public function show($id)
    {
        $express_company = $this->express_company_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $express_company['data']);
    }
}
